package com.flame.user.controller;

import com.flame.auth.core.annotation.HasAuth;
import com.flame.user.controller.vo.AccountPwdVo;
import com.flame.user.controller.vo.AccountVo;
import com.flame.user.service.AccountService;
import com.flame.core.protocol.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * flame_user_account 用户账号表
 *
 * @author 吴欣童
 * @since 2022-12-14 16:06:58
 */
@RestController
public class AccountController {

    @Autowired
    private AccountService accountService;

    @HasAuth("user:add")
    @Transactional(rollbackFor = Exception.class)
    @PostMapping("/user/account/generate")
    public Result generateAccount() {
        return Result.success(accountService.generateAccount());
    }

    @HasAuth("user:freeze")
    @Transactional(rollbackFor = Exception.class)
    @PostMapping("/user/account/freeze")
    public Result freezeAccount(@Validated @RequestBody AccountVo accountVo) {
        accountService.freezeAccount(accountVo);
        return Result.success();
    }

    @HasAuth("user:freeze")
    @Transactional(rollbackFor = Exception.class)
    @PostMapping("/user/account/unfreeze")
    public Result unFreezeAccount(@Validated @RequestBody AccountVo accountVo) {
        accountService.unFreezeAccount(accountVo);
        return Result.success();
    }

    @HasAuth("user:cancel")
    @Transactional(rollbackFor = Exception.class)
    @PostMapping("/user/account/cancel")
    public Result cancelAccount(@Validated @RequestBody AccountVo accountVo) {
        accountService.cancelAccount(accountVo);
        return Result.success();
    }

    @HasAuth("user:reset-pwd")
    @Transactional(rollbackFor = Exception.class)
    @PostMapping("/user/account/pwd/reset")
    public Result resetAccountPwd(@Validated @RequestBody AccountVo accountVo) {
        accountService.resetAccountPwd(accountVo);
        return Result.success();
    }

    @Transactional(rollbackFor = Exception.class)
    @PostMapping("/user/account/pwd/update")
    public Result updateAccountPwd(@Validated @RequestBody AccountPwdVo accountPwdVo) {
        accountService.updateAccountPwd(accountPwdVo);
        return Result.success();
    }
}
